<?php
class MeetingController extends CController {

	public $project;

	public function __construct($id, $module = null) {
		parent::__construct($id, $module);
	}

	public function filters() {
		return array (
			'CreateMeeting + create', 
			'EditMeeting + itemTree', 
			'EditMeeting + endOfMeeting', 
			'ShowMeeting + show' 
			);
	}

	public function filterCreateMeeting($filterChain)
	{
		if (!ProjectHelper::canEditProject($this->project))
		{
			throw new CHttpException(403,'Access Denied');
		}
		$filterChain->run();
	}

	public function filterEditMeeting($filterChain)
	{
		$meeting = Meetings::model()->findByPk($_GET['id']);
		if (! MeetingHelper::canEditMeeting($meeting))
		{
			throw new CHttpException(403,'Access Denied');
		}
		$filterChain->run();
	}

	public function filterShowMeeting($filterChain)
	{
		$meeting = Meetings::model()->findByPk($_GET['id']);
		if (! MeetingHelper::canAccessMeeting($meeting))
		{
			throw new CHttpException(403,'Access Denied');
		}
		$filterChain->run();
	}

	public function accessRules() {
		return array (
		array (
				'allow', // allow all users to perform 'list' and 'show' actions
				'actions' => array ('create'), 
				'expression' => 'ProjectHelper::canEditProject($this->project)'
				)
				);
	}

	public function init() {
		$this->project = Project::model()->findByPk($_GET['project_id']);
		$this->pageTitle = $this->project->title;
	}

	public function actions() {
		return array (
			'create' => 'adviser.controllers.meeting.CreateAction',
			'itemTree' => 'adviser.controllers.meeting.ItemTreeAction',
			'endOfMeeting' => 'adviser.controllers.meeting.EndOfMeetingAction',
			'previousObjectives' => 'adviser.controllers.meeting.PreviousObjectivesAction'
			);
	}

	public function actionShow() {
		$this->layout = 'project';
		$meeting = Meetings::model()->findByPk($_GET['id']);
		if ($meeting == null) {
			throw new CHttpException(404, "Meeting #".$_GET['id']." not found");
		}
		$this->project = $meeting->project;
		$tmp = $this->project->topics;
		$roots = array();
		$topics = array();

		if ($tmp != null){
			Yii::log("Converting #".$_GET['id'],CLogger::LEVEL_INFO, $category);
			while (count($tmp) > 0) {
				$topic = array_shift($tmp);
				$topic->isNewRecord = false;
				$topics[$topic->key] = $topic;
				$topic->checked = true;
				Yii::log("Found {$topic->key} in database",CLogger::LEVEL_TRACE, "Items");
				if ($topic->children != null) {
					$tmp = array_merge($topic->children, $tmp);
				}
			}
		}
		foreach ($topics as $topic){
			if ($topic->parent_key != null && array_key_exists($topic->parent_key, $topics)) {
				if (!isset($topic->id)) {
					$idx = $topic->idx;
					$topic->parent = $topics[$topic->parent_key];
					$topic->idx = $idx;
				}
			} else {
				$roots[] = $topic;
			}
		}


		$this->render('show', array (
			'project' => $this->project,
			'topics' => $topics,
			'roots' => $roots,
			'meeting' => $meeting
		));
	}

	public static function convert_date($date, $source_format, $display_format) {
		$d = CDateTimeParser::parse($date, $source_format);
		return Yii::app()->dateFormatter->format($display_format, $d);
	}

	public function dashed($text) {
		if (is_array($text)) {
			$s = "<span>";
			foreach($text as $t){
				$s .= $this->dashed($t."&nbsp;");
			}
			$s .= "</span>";
			return $s;
		} else {
			return "<div style='border-bottom: 1px lightgray dashed;'>$text</div>";
		}
	}
}
?>
